#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
typedef struct ListNode {
    int val;
    struct ListNode* next;
}L;
L* CreatListNode(int m, int n)
{
    int i = 1;
    L* newNode = (L*)malloc(sizeof(L));
    newNode->val = 1;
    L* head = newNode;
    head->next = NULL;
    L* cur = head;
    L* prev = head;
    while (--m)
    {
        L* newNode1 = (L*)malloc(sizeof(L));
        newNode1->val = (++i);
        cur->next = newNode1;
        cur = cur->next;
    }
    cur->next = head;
    cur = head;
    while (1)
    {
        int j = n;
        while (--j)
        {
            prev = cur;
            cur = cur->next;
        }
        L* next = cur->next;
        prev->next = cur->next;
        free(cur);
        cur = next;
        if (cur == prev)
        {
            break;
        }
    }
    return cur;
}
int main()
{
    int m = 0;
    int n = 0;
    scanf("%d%d", &m, &n);
    L* head = CreatListNode(m, n);
    printf("%d", head->val);
    return 0;
}